package com.yanqu.road.server.gameplayer.module.cricket.entity.dao;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.module.cricket.entity.DbUserCricketWaitSendBattleLog;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserCricketWaitSendBattleLogDaoImpl extends TempDataObjectDao<DbUserCricketWaitSendBattleLog> {

    public static UserCricketWaitSendBattleLogDaoImpl newInstance() {
        UserCricketWaitSendBattleLogDaoImpl dao = new UserCricketWaitSendBattleLogDaoImpl();
        return dao;
    }

    public static String insertSql = "insert into `t_u_cricket_wait_handle_battle_log`(originId,toServerId,userId,sync,battleLog,createTime,updateTime) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DbUserCricketWaitSendBattleLog data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<Integer, DbParameter>();
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getOriginId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getToServerId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getSync()));
        String encodeToString = Base64.getEncoder().encodeToString(data.getBattleLog().toByteArray());
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, encodeToString));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(insertSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "update `t_c_cricket_user` set `sync`=? where originId = ?";
    @Override
    protected boolean updateData(DbUserCricketWaitSendBattleLog data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<Integer, DbParameter>();
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getSync()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getOriginId()));
        if (getDb().executeNoneQuery(updateSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DbUserCricketWaitSendBattleLog parseData(ResultSet rs) throws SQLException {
        DbUserCricketWaitSendBattleLog data = DbUserCricketWaitSendBattleLog.getInstance();
        data.setOriginId(rs.getLong("originId"));
        data.setToServerId(rs.getLong("toServerId"));
        data.setUserId(rs.getLong("userId"));
        data.setSync(rs.getInt("sync"));
        String cricketBattleLogString = rs.getString("battleLog");
        byte[] decode = Base64.getDecoder().decode(cricketBattleLogString);
        try {
            CricketProto.CricketDefendBattleLogReqMsg cricketBattleLog = CricketProto.CricketDefendBattleLogReqMsg.parseFrom(decode);
            data.setBattleLog(cricketBattleLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
        data.setCreateTime(rs.getLong("createTime"));
        data.setUpdateTime(rs.getLong("updateTime"));

        return data;
    }


    public ConcurrentHashMap<Long, DbUserCricketWaitSendBattleLog> getDataMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        ConcurrentHashMap<Long, DbUserCricketWaitSendBattleLog> dataMap = new ConcurrentHashMap<>();
        String selectSql = "select * from t_u_cricket_battle_log where sync = 0";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            rs = call.executeQuery();
            while (rs.next()) {
                DbUserCricketWaitSendBattleLog data = parse(rs);
                dataMap.put(data.getOriginId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public boolean saveData(DbUserCricketWaitSendBattleLog data) {
        if (data.isUpdateOption()) {
            if (data.isNeedInsert()) {
                if (addData(data)) {
                    data.setNeedInsert(false);
                }
            }
        }
        return true;
    }

}
